package com.mt.skyhoist.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mt.skyhoist.R;
import com.mt.skyhoist.ui.adapter.listener.OnRecyclerViewItemClickListener;
import com.mt.sdk.help.bean.BluetoothMsg;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by zhaodaizheng on 17/4/20.
 */

public class BluetoothItemAdapter extends BaseAdapter<BluetoothMsg, BluetoothItemAdapter.BluetoothMsgViewHolder> {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public BluetoothItemAdapter(Context context, List<BluetoothMsg> data) {
        super(context, R.layout.item_bluetooth, data);
    }


    @Override
    public BluetoothMsgViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bluetooth, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onRecyclerViewItemClickListener != null) {
                    //注意这里使用getTag方法获取数据
                    if (v.getTag() == null) {
                    } else {
                        onRecyclerViewItemClickListener.onItemClick(v, (Integer) v.getTag());
                    }
                }
            }
        });
        return new BluetoothMsgViewHolder(v);
    }

    @Override
    protected void convert(BluetoothMsgViewHolder helper, BluetoothMsg item) {
        helper.tvName.setText(item.getName());
        helper.tvRssi.setText("rssi = "+item.getRssi());
        helper.tvAddress.setText("Mac:"+item.getMacAdress());
    }

    public static class BluetoothMsgViewHolder extends RecyclerView.ViewHolder {
        TextView tvRssi;
        TextView tvName;
        TextView tvAddress;
        public BluetoothMsgViewHolder(View contentView) {
            super(contentView);
            tvRssi = contentView.findViewById(R.id.tv_rssi);
            tvName = contentView.findViewById(R.id.tv_name);
            tvAddress = contentView.findViewById(R.id.tv_address);
        }
    }


    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }
}
